from odoo import api, fields, models, _


class SubcontractProductSupplierinfo(models.Model):
    _name = 'subcontract.product.supplierinfo'
    _inherit = ['portal.mixin', 'mail.thread.main.attachment', 'mail.activity.mixin']
    _description = '分包供应商价格表'

    name = fields.Char()
    partner_id = fields.Many2one('res.partner', string='Vendor')
    delay = fields.Integer(string='交货提前时间')
    product_id = fields.Many2one('product.product', 'Product', domain=[('detailed_type', '!=', 'service')])
    service_product_id = fields.Many2one('product.product', string='Product Service', domain=[('detailed_type', '=', 'service')])
    pcs_price = fields.Float(string='Pcs Price')
    panel_price = fields.Float(string='Panel Price', digits="Product Unit of Measure")
    set_price = fields.Float(string='Set Price')
    area_price_unit = fields.Float(string='Area Price Unit', digits="Product Unit of Measure")
    area_pnl_qty = fields.Float(string='Active PANEL/M²', digits="Product Unit of Measure")
    start_date = fields.Date(string='有效开始日期')
    stop_date = fields.Date(string='有效截止期')
    uom_po_id = fields.Many2one('uom.uom', related='service_product_id.uom_po_id', string='Purchase Uom')
    uom_name = fields.Char(related='service_product_id.uom_po_id.name', string="Uom Name")
    # uom_value_ids = fields.One2many('product.uom.value',
    #                                 'product_id', 'Product Uom Value', copy=True)
    ratio = fields.Char(string='Ratio', help='产品单位PCS/SET/PANEL的比值')
    active = fields.Boolean(default=True)
    s_service_pricing_method = fields.Selection([('panel', 'Panel'), ('area', 'Area')],
                                                related='service_product_id.s_service_pricing_method',
                                                string='Subcontracting Pricing Method')

    @api.depends('product_id', 'write_date')
    def _compute_ratio(self):
        if not self.product_id:
            self.ratio = '1/1/1'
        uom_value_ids = self.product_id.uom_value_ids
        if not uom_value_ids:
            return
        set_r = '1.0'
        pcs_r = '1.0'
        pnl_r = '1.0'
        for uom in uom_value_ids:
            if uom.uom_id.name == 'SET':
                set_r = uom.uom_value
            if uom.uom_id.name == 'PCS':
                pcs_r = uom.uom_value
            if uom.uom_id.name == 'PANEL':
                pnl_r = uom.uom_value
        self.ratio = '{}/{}/{}'.format(pcs_r, set_r, pnl_r)
        # if self.uom_name == 'PCS':
        #     self.onchange_price1()
        # if self.uom_name == 'SET':
        #     self.onchange_price2()
        # if self.uom_name == 'PANEL':
        #     self.onchange_price3()

    def get_unit_number(self, product_id):
        uom_panel_width = self.env.ref('pcbmanufact.uom_panel_width')
        uom_panel_length = self.env.ref('pcbmanufact.uom_panel_length')
        uom_ids = product_id.uom_value_ids.mapped('uom_id')
        pnl_width = 0
        pnl_length = 0
        if uom_panel_width in uom_ids and uom_panel_length in uom_ids:
            pnl_width = product_id.uom_value_ids.filtered(lambda x: x.uom_id.id == uom_panel_width.id).uom_value
            pnl_length = product_id.uom_value_ids.filtered(lambda x: x.uom_id.id == uom_panel_length.id).uom_value
        return pnl_length, pnl_width

    @api.onchange('product_id')
    def onchange_area_pnl_qty(self):
        if self.product_id:
            pnl_length, pnl_width = self.get_unit_number(self.product_id)
            self.area_pnl_qty = round(pnl_length * pnl_width / 1000000, 3)
        else:
            self.area_pnl_qty = 0.0

    @api.onchange('partner_id')
    def onchange_name(self):
        if self.partner_id:
            self.name = self.partner_id.complete_name

    def _get_ratio(self):
        ratio = self.ratio.split('/')
        pcs_r = float(ratio[0]) if float(ratio[0]) else 1.0
        set_r = float(ratio[1]) if float(ratio[1]) else 1.0
        pnl_r = float(ratio[2]) if float(ratio[2]) else 1.0
        return pcs_r, set_r, pnl_r

    # @api.onchange('pcs_price')
    # def onchange_price1(self):
    #     # "1/4/24"
    #     # if self.uom_name != 'PCS': return
    #     pcs_r, set_r, pnl_r = self._get_ratio()
    #     if self.pcs_price:
    #         self.set_price = self.pcs_price * set_r
    #         self.panel_price = self.pcs_price * pnl_r
    #     else:
    #         self.panel_price = 0
    #         self.pcs_price = 0
    #         self.set_price = 0
    #
    # @api.onchange('panel_price')
    # def onchange_price2(self):
    #     # "1/4/24"
    #     # if self.uom_name != 'PANEL': return
    #     pcs_r, set_r, pnl_r = self._get_ratio()
    #     if self.panel_price:
    #         self.set_price = self.panel_price / pnl_r * set_r
    #         self.pcs_price = self.panel_price / pnl_r
    #     else:
    #         self.panel_price = 0
    #         self.pcs_price = 0
    #         self.set_price = 0
    #
    # @api.onchange('set_price')
    # def onchange_price3(self):
    #     # "1/4/24"
    #     # if self.uom_name != 'SET': return
    #     pcs_r, set_r, pnl_r = self._get_ratio()
    #     if self.set_price:
    #         self.panel_price = self.set_price / set_r * pnl_r
    #         self.pcs_price = self.set_price / set_r
    #     else:
    #         self.panel_price = 0
    #         self.pcs_price = 0
    #         self.set_price = 0

    @api.model_create_multi
    def create(self, vals_list):
        res = super(SubcontractProductSupplierinfo, self).create(vals_list)
        active_id = self._context.get('active_id')
        if active_id:
            apply_line = self.env['sb.subcontracting.apply.line'].browse([active_id])
            apply_line.supplierinfo_id = res.id
        return res
